import {
  getSetting,
  openSetting,
  chooseAddress,
  showModal,
  showToast
} from "../../utils/asyncWx.js";
import regeneratorRuntime from "../../lib/runtime/runtime";
import { request } from "../../request/index";

Page({
  data: {
    address: {},
    cartGoods: [],
    totalPrice: 0,
    totalNum: 0
  },

  onShow() {
    const address = wx.getStorageSync("address") || {};
    address.address =
      address.provinceName +
      address.cityName +
      address.countryName +
      address.detailInfo;
    let goods = wx.getStorageSync("cart") || [];
    let totalNum = 0;
    let totalPrice = 0;
    goods.forEach(v => {
      if (v.check) {
        totalPrice += v.goods_price * v.num;
        totalNum += v.num;
      }
    });
    goods = goods.filter(v => v.check);
    this.setData({
      cartGoods: goods,
      totalNum,
      totalPrice
    });
  },
  async handleOrderPay() {
    // const token = wx.getStorageSync("token");
    // if (!token) {
    //   wx.navigateTo({
    //     url: "/pages/auth/index"
    //   });
    // } else {
    //   try {
    //     const token = wx.getStorageSync('token')
    //   const header = {Authorization: token}
    //   const order_price = this.data.totalPrice
    //   const consignee_addr = this.data.address.address
    //   const goods=  []
    //   cartGoods.forEach(v=> goods.push({
    //     goods_id = v.goods_id,
    //     goods_number = v.goods_number,
    //     goods_price = v.goods_price
    //   }))
    //   const res = await request({url: '/my/orders/create', data: goods, header, method: "POST"})
    //   console.log(res)
    //   } catch(err) {
    //     console.log(err)
    //   }
    //   await showToast('支付成功')
    //   wx.navigateTo({
    //     url: '/pages/order/index'
    //   });
    // }
    const cart = wx.getStorageSync("cart");
    let cart2 = cart.filter(v => !v.check);
    wx.setStorageSync("cart", cart2);

    await showToast("支付成功");
    function gogogo() {
      wx.navigateTo({
        url: "/pages/order/index"
      });
    }
    setTimeout(gogogo, 1000);
  }
});
